package vip.mall.modular.goods.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import vip.mall.common.result.CommonPageResult;
import vip.mall.modular.goods.entity.Goods;
import vip.mall.modular.goods.mapper.GoodsMapper;
import vip.mall.modular.goods.params.AddGoods;
import vip.mall.modular.goods.params.GoodsPageParams;
import vip.mall.modular.goods.service.GoodsService;
import vip.mall.modular.goods.vo.GoodsVo;
import vip.mall.modular.specialist.entity.Specialist;
import vip.mall.modular.specialist.service.SpecialistService;

import java.util.ArrayList;
import java.util.List;

/**
 * @author:ASUS
 * @date:2025/2/10 10:34
 */
@Service
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements GoodsService {
    @Resource
    private GoodsMapper goodsMapper;
    @Resource
    private SpecialistService specialistService;
    @Override
    public boolean add(AddGoods addGoods) {
        Goods goods = BeanUtil.copyProperties(addGoods, Goods.class);
        return save(goods);
    }
    @Override
    public List<Goods> findGoodsInfo(String GoodsInfo) {
        LambdaQueryWrapper<Goods> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(Goods::getGoodsName, GoodsInfo);
        return goodsMapper.selectList(lambdaQueryWrapper);
    }
    @Override
    public CommonPageResult pageList(GoodsPageParams goodsPageParams) {
        LambdaQueryWrapper<Goods> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (ObjectUtil.isNotEmpty(goodsPageParams.getGoodsName()))
            lambdaQueryWrapper.like(Goods::getGoodsName, goodsPageParams.getGoodsName());
        if (ObjectUtil.isNotEmpty(goodsPageParams.getTypeId()))
            lambdaQueryWrapper.eq(Goods::getTypeId, goodsPageParams.getTypeId());
        Page<Goods> page=new Page<>(goodsPageParams.getPageNum(),goodsPageParams.getPageSize());
        Page<Goods> goodsPage = this.page(page,lambdaQueryWrapper);
        if(ObjectUtil.isNotEmpty(goodsPage))
            return CommonPageResult.builder()
                    .data(goodsPage.getRecords())
                    .pageNum((int) goodsPage.getCurrent())
                    .pageSize((int) goodsPage.getSize())
                    .totalPage((int) goodsPage.getPages())
                    .total(goodsPage.getTotal())
                    .build();
        return null;
    }
    @Override
    public List<GoodsVo> showGoods(String typeId) {
        LambdaQueryWrapper<Goods> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (ObjectUtil.isNotEmpty(typeId))
            lambdaQueryWrapper.eq(Goods::getTypeId, typeId);
        List<Goods> goods = this.list(lambdaQueryWrapper);
        LambdaQueryWrapper<Specialist> lambdaQueryWrapperSp = new LambdaQueryWrapper<>();
        List<GoodsVo> goodsVos = new ArrayList<>();
        for (Goods good : goods){
            lambdaQueryWrapperSp.eq(Specialist::getGoodsId,good.getId());
            List<Specialist> specialists = specialistService.list(lambdaQueryWrapperSp);
            GoodsVo goodsVo = BeanUtil.copyProperties(good, GoodsVo.class);
            goodsVo.setSpecialists(specialists);
            goodsVos.add(goodsVo);
        }
        return goodsVos;
    }
}
